package com.mpo.web.common.util;

import java.util.ArrayList;
import java.util.List;

import javax.faces.model.SelectItem;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.Transformer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.mpo.web.common.util.UtilSelectItems;

import com.mpo.common.util.UtilReflection;

public final class UtilSelectItems {
	
	private static final Logger slf4jLogger = LoggerFactory.getLogger(UtilSelectItems.class);
	
	private static final UtilSelectItems INSTANCIA = new UtilSelectItems();
	
	/**
	 * Constructor privado
	 */
	private UtilSelectItems() {}
	
	/**
	 * Devolvemos la instancia de la clase
	 * @return INSTANCIA
	 */
	public static UtilSelectItems getInstancia(){
		return INSTANCIA;
	}
	
public <T> List<SelectItem> cargarSelectItemsGenerico (List<T> listData, final String idPrimaryKey, final String name) {
		
		slf4jLogger.info("cargarSelectItemsGenerico");
		
		List<SelectItem> listSelectItem = new ArrayList<SelectItem>();
		
		CollectionUtils.collect(listData, new Transformer() {
			@Override
			public Object transform(final Object arg0) {
				return new SelectItem(UtilReflection.invocarGet(arg0, idPrimaryKey), UtilReflection.invocarGet(arg0, name));
			}
		}, listSelectItem);
		
		return listSelectItem;
	}
}
